using System;
using System.Collections.Generic;
using System.Text;
using WhsFacebook.Whs.ErrorReporting;

namespace WhsFacebook.Whs.Service.ErrorReporting
{
    public sealed class ServiceErrorReporting
    {
        private static readonly ServiceErrorReporting _instance = new ServiceErrorReporting();

        public static ServiceErrorReporting Instance
        {
            get { return _instance; }
        }

        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(ServiceErrorReporting));
        private int _lastSubmittedErrorIndex = -1;

        public int SubmittedErrors
        {
            get;
            private set;
        }

        public bool IsSubmitting
        {
            get;
            private set;
        }

        public float SubmittingProgress
        {
            get;
            private set;
        }

        public int UnsubmittedErrors
        {
            get
            {
                return ErrorReportingSingleton.Instance.GetUnsubmittedErrorCount();
            }
        }

        private ServiceErrorReporting()
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Creating singleton");

            try
            {
                // load state
                try
                {
                    this.SubmittedErrors = Properties.States.Default.SubmittedErrors;
                }
                catch
                {
                    _log.Warn("Failed to read SubmittedErrors state");

                    this.SubmittedErrors = 0;
                }

                this.IsSubmitting = false;
                this.SubmittingProgress = 1.0f;

                // register with error reporting
                ErrorReportingSingleton.Instance.OnErrorSubmitted += 
                    new ErrorReportingSingleton.OnErrorSubmittedDelegate(OnErrorSubmitted);
	
                // start error submitting
                if (Properties.Settings.Default.EnableErrorReportsSubmission)
                    ErrorReportingSingleton.Instance.StartSubmittingErrors();
            }
            catch (Exception e)
            {
                _log.Fatal("Failed to create singleton", e);

                throw;
            }

        }

        private void OnErrorSubmitted(ErrorReportingSingleton.ErrorInfo error, int index, int totalErrorCount)
        {
            // handle submitting start state
            if (index <= 0)
            {
                this.IsSubmitting = true;
                this.SubmittingProgress = 0.0f;

                return;
            }

            // increase counter
            if (_lastSubmittedErrorIndex != index)
            {
                this.SubmittedErrors++;

                _lastSubmittedErrorIndex = index;

                // set percentage
                this.IsSubmitting = true;
                this.SubmittingProgress = ((float)index / totalErrorCount);

                // save state
                Properties.States.Default.SubmittedErrors = this.SubmittedErrors;
                Properties.States.Default.Save();
            }

            // handle completed state
            if (index >= totalErrorCount)
            {
                this.IsSubmitting = false;
                this.SubmittingProgress = 1.0f;
            }
        }
    }
}